package com.lebang.controller.api.draw;

import com.alibaba.fastjson.JSON;
import com.lebang.common.Require;
import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.feign.draw.DrawClickFeign;
import com.lebang.model.MoneyCategory;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebang.service.buss.MemberService;
import com.lebang.service.buss.MemberTradeService;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

/**
 * 签到
 *
 * @Author: xwj
 * @Date: 2020/1/8 13:52
 * @Version 1.0
 */
@Api(tags = "签到")
@RestController
@RequestMapping("/api/draw/click")
public class DrawClickRecordApi {
    @Autowired
    private DrawClickFeign drawClickFeign;
    @Autowired
    private MemberService memberService;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Autowired
    private MemberTradeService tradeService;
    @Autowired
    private RedisClient redisClient;


    /**
     * 每日签到状态
     */
    @ApiOperation(value = "每日签到状态")
    @PostMapping("/status")
    public JsonData status(HttpServletRequest request){
        RequestUserInfo requestUserInfo =new RequestUserInfo(request);
        return drawClickFeign.status(requestUserInfo);
    }



    /**
     * 获取按钮的签到状态
     * @param request
     * @return
     */
    @ApiOperation(value = "签到状态")
    @PostMapping("/info")
    public JsonData info(HttpServletRequest request){
        RequestUserInfo requestUserInfo =new RequestUserInfo(request);
        LbbMember lbbMember = memberService.selectMemberById(requestUserInfo.getMemberId());
        Integer isNewMember = checkIsNewMember(lbbMember.getMemberId());
        return drawClickFeign.info(requestUserInfo,isNewMember);


    }


    /**
     * 签到
     */
    @Require
    @ApiOperation(value = "签到")
    @PostMapping("/click")
    public JsonData click(HttpServletRequest request,Integer recordId){
        RequestUserInfo requestUserInfo =new RequestUserInfo(request);
        RequestHeaders headers = new RequestHeaders(request);
        JsonData jsonData = drawClickFeign.click(requestUserInfo,recordId);
        if(jsonData.isSuccess()){
            Integer awardType = JSON.parseObject(JSON.toJSONString(jsonData.getData())).getIntValue("awardType");
            Integer award = JSON.parseObject(JSON.toJSONString(jsonData.getData())).getIntValue("award");
            Integer clickDays = JSON.parseObject(JSON.toJSONString(jsonData.getData())).getIntValue("clickDays");
            if(awardType==1){
                //喵币
                pointService.addPoint(requestUserInfo.getMemberId(), award, TaskCategory.WISH_CAT_CLICK, headers, "");
                memberNoticeService.addNoticeAndPush("公交打卡", "打卡领取" + award + "金币", headers.getUserid(), headers);
                return Callback.success("200", "恭喜获得"+award+"金币!,已连续签到"+clickDays+"天!");
            }else{
                return Callback.fail("201", "签到失败,奖励类型不存在");
            }
        }

        return jsonData;
    }




    private Integer checkIsNewMember(String memberId) {
        LocalDate now = LocalDate.now();
        if(memberService.selectCreateById(memberId).toLocalDate().isEqual(now)){
            return Whether.YES.getDbIndex();
        }
        return Whether.NO.getDbIndex();
    }


}
